import threading # 多线程
from mylib.net_com import * # 网络连接库
#from mylib.icm import * # ICM传感器

# 断线重连（By Xia）
flag = 1
while flag:
    try:
        communication = com('ADMIN', '192.168.1.134', 1883, 60) # 初始化MQTT链接
        if communication != None:
            flag = 0
    except:
        pass
#testicm = ICM()


# 定义主要线程

def inertial_positioning(): # 惯性定位
    # 全局变量：
    #     inertial_location：(position_x:int, position_y:int) 定位坐标
    while True:
        print("ip")
        pass

def RSSI_positioning(): # RSSI定位
    # 全局变量：
    #     RSSI_location: (position_x:int, position_y:int, deviation:int) 定位坐标及误差
    while True:
        print("rssi")
        pass

def sent_communicating(): # 发送通讯
    while True:
        communication.sent(1, 1, 45, None, False)

def receive_communicating(): # 接收通讯
    while True:
        data = communication.receive
        print(data)

def auto_pilot(): # 自动驾驶
    # 全局变量：
    #     next_action：(turning:int, target_speed:int) 下步动作（分别是目标方向、目标速度）
    #     emergency：true/false 是否有紧急情况
    while True:
        print("ap")
        pass


# 绑定线程

Thread_inertial_positioning = threading.Thread(target=inertial_positioning)
Thread_RSSI_positioning = threading.Thread(target=RSSI_positioning)
Thread_sent_communicating = threading.Thread(target=sent_communicating)
Thread_receive_communicating = threading.Thread(target=receive_communicating)
Thread_auto_pilot = threading.Thread(target=auto_pilot)

# 开始线程

Thread_inertial_positioning.start()
Thread_RSSI_positioning.start()
Thread_sent_communicating.start()
Thread_receive_communicating.start()
Thread_auto_pilot.start()
